package com.sax.answer.handler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sax.answer.entity.WeiBo;

public class WeiBoHandler extends DefaultHandler{
	
    private List<WeiBo> list=new ArrayList<WeiBo>();
    private WeiBo weibo=null;
    private int size;
    private String value;
    
    public List<WeiBo> getWeiBoList(){
		return list;
    	
    }
    
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		System.out.println("begin...");
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName,attributes);
		if("RECORD".equalsIgnoreCase(qName)){
			weibo=new WeiBo();
			size++;
			System.out.println("the "+size+"条微博开始读入");
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		value=new String(ch,start,length);
		
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		SimpleDateFormat formatter = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");
		Date date;
		super.endElement(uri, localName, qName);
		if("id".equalsIgnoreCase(qName)){
			weibo.setId(value);
		}
		else if("article".equalsIgnoreCase(qName)){
			weibo.setArticle(value);
		}
		else if("discuss".equalsIgnoreCase(qName)){
				weibo.setDiscuss(value);
		}
		else if("insertTime".equalsIgnoreCase(qName)){
			date=new Date();
			try {
				date=formatter.parse(value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			weibo.setInsertTime(new java.sql.Date(date.getTime()));
		}
		else if("origin".equalsIgnoreCase(qName)){
			weibo.setOrigin(value);
		}
		else if("person_id".equalsIgnoreCase(qName)){
			weibo.setPersonId(value);
		}
		else if("time".equalsIgnoreCase(qName)){
			date=new Date();
			try {
				date=formatter.parse(value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			weibo.setTime(new java.sql.Date(date.getTime()));
		}
		else if("transmit".equalsIgnoreCase(value)){
			try{
				weibo.setTransmit(Integer.valueOf(value));
			}catch(NumberFormatException e){
				weibo.setTransmit(0);
			}
			
		}
		else if("RECORD".equalsIgnoreCase(qName)){
			list.add(weibo);
			System.out.println(weibo.toString());
			weibo=null;
			System.out.println("the "+size+"条微博读入结束");
		}
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
		System.out.println("end....");
	}
}
